#include "system_setting.h"
#include "ui_system_setting.h"
#include <QPoint>
SystemSetting::SystemSetting(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SystemSetting)
{
    ui->setupUi(this);
    static  int pbUserClicked = 1;
    connect(ui->pb_systemSettingReturnMenu, &QPushButton::clicked, this, &SystemSetting::pushReturnMenu);

    ui->widget->setAutoFillBackground(true);
    QPalette palette  = ui->widget->palette();
    palette.setColor(QPalette::Background, Qt::white);
    ui->widget->setPalette(palette);


    ui->stackedWidget->setCurrentIndex(0);
    ui->stackedWidget_3->setCurrentIndex(0);
    QPushButton* buttons[] = { ui->pb_systemSetting, ui->pb_userSetting,
                               ui->pb_passwdSetting, ui->pb_vision, ui->pb_nextPage };

    for (int i = 0; i < 5; ++i) {
        connect(buttons[i], &QPushButton::clicked, this, [this, i]() {
            if (i < 4) {
                ui->stackedWidget_3->setCurrentIndex(i);
            } else {
                int page = ui->stackedWidget->currentIndex();
                if (page < 3) {
                    ui->stackedWidget->setCurrentIndex(page + 1);
                }
            }
        });
    }

    ui->label_2->setText("阶层表示设定");
    connect(ui->pb_lastPage, &QPushButton::clicked, this, [&](){
        int page = ui->stackedWidget->currentIndex();
        if(page >= 0) {
            ui->stackedWidget->setCurrentIndex(--page);
        }

    });
    for (QPushButton* button : {ui->pushButton_2, ui->pushButton_3, ui->pushButton_4, ui->pushButton_5}) {
        connect(button, &QPushButton::clicked, this, [=](){
            button->setText(button->text() == "OFF" ? "ON" : "OFF");
        });
    }


    ui->widget_3->hide();

    for (QPushButton* userButton : {ui->pb_user1, ui->pb_user2, ui->pb_user3, ui->pb_user4}) {
        connect(userButton, &QPushButton::clicked, this, [=](){
            ui->widget_3->show();
            ui->lineEdit_2->setFocus();
            pbUserClicked = userButton->objectName().right(1).toInt();
        });
    }

    connect(ui->toolButton_15,&softkeys::close_windows,this,[&](){
        ui->widget_3->hide();
        ui->lineEdit_2->setText("");

    });

    connect(ui->toolButton_17,&softkeys::close_windows,this,[&](){
        ui->widget_3->hide();
        setPassword(pbUserClicked);
        ui->lineEdit_2->setText("");
    });
    softKeyboard = new SoftKeyboard();
    ui->lineEdit->installEventFilter(this);
}

void SystemSetting::setPassword(const int &pbUserClicked) {
    QString tempPwd = ui->lineEdit_2->text();

    if (pbUserClicked >= 1 && pbUserClicked <= 4) {
        userPasswd[pbUserClicked - 1] = tempPwd;
    }
}
void SystemSetting::displayUserLevel()
{
    QString tempStr = "用户";
    ui->label_3->setText(tempStr+userLevel);
}

SystemSetting::~SystemSetting()
{
    delete ui;
}

void SystemSetting::pushReturnMenu()
{
    emit returnMenu();
}


bool SystemSetting::eventFilter(QObject *watched, QEvent *event)
{
    if(event->type()==QEvent::MouseButtonRelease)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
        if(mouseEvent->button() == Qt::LeftButton)
        {
            QPoint point = this->pos();
            if(watched == ui->lineEdit)
            {
                softKeyboard->hideInputBufferArea(ui->lineEdit);
                softKeyboard->resize(100,120);
                softKeyboard->move(point+ui->lineEdit->pos());
                softKeyboard->show();
            }
        }
    }
    return QWidget::eventFilter(watched,event);
}
